package com.quzi.kettle.utils;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnDismissListener;
import android.graphics.drawable.ColorDrawable;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.view.WindowManager.LayoutParams;
import android.widget.LinearLayout;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TextView;

import com.quzi.kettle.R;
import com.quzi.kettle.view.CustomDialogInterface;

public class AlertUtil {

	/**
	 * Show Alert Dialog
	 * 
	 * @param context
	 * @param titleId
	 * @param messageId
	 */
	public static void showAlert(Context context, int titleId, int messageId) {

		showAlert(context, context.getString(titleId),
				context.getString(messageId));

	}

	/**
	 * Show Alert Dialog
	 * 
	 * @param context
	 * @param titleId
	 * @param message
	 */
	public static void showAlert(Context context, int titleId, String message) {

		showAlert(context, context.getString(titleId), message);

	}

	/**
	 * Show Alert Dialog
	 * 
	 * @param context
	 * @param title
	 * @param message
	 */
	public static void showAlert(Context context, String title, String message) {
		Dialog dlg = new AlertDialog.Builder(context)
				.setIcon(android.R.drawable.ic_dialog_alert).setTitle(title)
				.setPositiveButton(android.R.string.ok, null)
				.setMessage(message).create();

		dlg.show();
	}

	/**
	 * Show Alert Dialog
	 * 
	 * @param context
	 * @param titleId
	 * @param messageId
	 * @param positiveButtontxt
	 * @param positiveListener
	 * @param negativeButtontxt
	 * @param negativeListener
	 */
	public static void showAlert(Context context, int titleId, int messageId,
			CharSequence positiveButtontxt,
			DialogInterface.OnClickListener positiveListener,
			CharSequence negativeButtontxt,
			DialogInterface.OnClickListener negativeListener) {

		showAlert(context, context.getString(titleId),
				context.getString(messageId), positiveButtontxt,
				positiveListener, negativeButtontxt, negativeListener);

	}

	/**
	 * Show Alert Dialog
	 * 
	 * @param context
	 * @param title
	 * @param message
	 * @param positiveButtontxt
	 * @param positiveListener
	 * @param negativeButtontxt
	 * @param negativeListener
	 */
	public static void showAlert(Context context, String title, String message,
			CharSequence positiveButtontxt,
			DialogInterface.OnClickListener positiveListener,
			CharSequence negativeButtontxt,
			DialogInterface.OnClickListener negativeListener) {
		Dialog dlg = new AlertDialog.Builder(context)
				// .setIcon(android.R.drawable.ic_dialog_alert)
				.setTitle(title)
				.setPositiveButton(positiveButtontxt, positiveListener)
				.setNegativeButton(negativeButtontxt, negativeListener)
				.setMessage(message).setCancelable(false).create();

		dlg.show();
	}

	/**
	 * Show Alert Dialog
	 * 
	 * @param context
	 * @param title
	 * @param message
	 * @param positiveButtontxt
	 * @param positiveListener
	 * @param negativeButtontxt
	 * @param negativeListener
	 * @param neutralButtontxt
	 * @param neutralListener
	 */
	public static void showAlert(Context context, String title, String message,
			CharSequence positiveButtontxt,
			DialogInterface.OnClickListener positiveListener,
			CharSequence negativeButtontxt,
			DialogInterface.OnClickListener negativeListener,
			CharSequence neutralButtontxt,
			DialogInterface.OnClickListener neutralListener) {
		Dialog dlg = new AlertDialog.Builder(context)
				// .setIcon(android.R.drawable.ic_dialog_alert)
				.setTitle(title)
				.setPositiveButton(positiveButtontxt, positiveListener)
				.setNegativeButton(negativeButtontxt, negativeListener)
				.setNeutralButton(neutralButtontxt, neutralListener)
				.setMessage(message).setCancelable(false).create();

		dlg.show();
	}

	/**
	 * Show Alert Dialog
	 * 
	 * @param context
	 * @param title
	 * @param message
	 * @param positiveButtontxt
	 * @param positiveListener
	 */
	public static void showAlert(Context context, String title, String message,
			CharSequence positiveButtontxt,
			DialogInterface.OnClickListener positiveListener) {
		Dialog dlg = new AlertDialog.Builder(context)
				.setIcon(android.R.drawable.ic_dialog_alert).setTitle(title)
				.setPositiveButton(positiveButtontxt, positiveListener)
				.setMessage(message).setCancelable(false).create();

		dlg.show();
	}

	// static AlertDialog alertDialog;

	static Dialog dialog;

	public static int dip2px(Context context, float dipValue) {
		final float scale = context.getResources().getDisplayMetrics().density;
		return (int) (dipValue * scale + 0.5f);
	}

	public static int px2dip(Context context, float pxValue) {
		final float scale = context.getResources().getDisplayMetrics().density;
		return (int) (pxValue / scale + 0.5f);
	}

	public static void ShowCustomDialog(Context context, LinearLayout lin,
			LayoutInflater inflater, String title, int value,
			final String unit, int maxValue, final int minValue,
			final int stepValue, final CustomDialogInterface handle) {
		maxValue = maxValue - minValue;
		value = value - minValue;
		View dialoglayout = inflater.inflate(R.layout.dialog_ctl, null);
		dialog = new Dialog(context);
		dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
		dialog.setContentView(dialoglayout);

		dialog.setOnDismissListener(new OnDismissListener() {

			@Override
			public void onDismiss(DialogInterface arg0) {
				// TODO Auto-generated method stub
				handle.dissmiss();
			}
		});

		// int divierId = dialog.getContext().getResources()
		// .getIdentifier("android:id/titleDivider", null, null);
		// View divider = dialog.findViewById(divierId);
		// divider.setBackgroundColor(context.getResources().getColor(
		// R.color.transparent));

		Window window = dialog.getWindow();

		WindowManager.LayoutParams wlp = window.getAttributes();
		window.setBackgroundDrawable(new ColorDrawable(
				android.graphics.Color.TRANSPARENT));
		wlp.gravity = Gravity.BOTTOM;
		wlp.x = 0;
		wlp.y = 0;
		wlp.width = LayoutParams.FILL_PARENT;

		wlp.height = lin.getHeight();

		wlp.flags &= ~WindowManager.LayoutParams.FLAG_DIM_BEHIND;
		window.setAttributes(wlp);

		dialoglayout.findViewById(R.id.cancle).setOnClickListener(
				new OnClickListener() {

					@Override
					public void onClick(View arg0) {
						if (dialog.isShowing()) {
							dialog.dismiss();
						}
						handle.onLeftClicked();
					}
				});
		dialoglayout.findViewById(R.id.confirm).setOnClickListener(
				new OnClickListener() {

					@Override
					public void onClick(View arg0) {
						if (dialog.isShowing()) {
							dialog.dismiss();
						}
						handle.onRightClicked();
					}
				});
		final TextView valueText = (TextView) dialoglayout
				.findViewById(R.id.show_text);
		//valueText.setText(minValue + " " + unit );
		SeekBar seek = (SeekBar) dialoglayout.findViewById(R.id.seekbar);
		seek.setMax(maxValue / stepValue);
		seek.setProgress(value / stepValue);
		valueText.setText(value + minValue + " " + unit);
		seek.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {

			@Override
			public void onStopTrackingTouch(SeekBar arg0) {
				// TODO Auto-generated method stub

			}

			@Override
			public void onStartTrackingTouch(SeekBar arg0) {
				// TODO Auto-generated method stub

			}

			@Override
			public void onProgressChanged(SeekBar arg0, int value, boolean arg2) {
				// TODO Auto-generated method stub
				handle.onProgresssChanged(value * stepValue + minValue);
				valueText.setText((value * stepValue) + minValue + " " + unit);
			}
		});
		TextView titleText = (TextView) dialoglayout.findViewById(R.id.title);
		titleText.setText(title);
		dialog.show();
	}

	public static void ShowCustomDialog(Context context, LinearLayout lin,
			LayoutInflater inflater, String title, int value,
			final String unit, int maxValue, final int stepValue,
			final CustomDialogInterface handle) {

		ShowCustomDialog(context, lin, inflater, title, value, unit, maxValue,
				0, stepValue, handle);
	}

	public static void showMyDialog(Context context, LayoutInflater inflater,
			String title) {
		View dialoglayout = inflater.inflate(R.layout.dialog_ctl, null);
		dialog = new Dialog(context);
		dialog.setContentView(dialoglayout);
		dialog.show();

		int divierId = dialog.getContext().getResources()
				.getIdentifier("android:id/titleDivider", null, null);
		View divider = dialog.findViewById(divierId);
		divider.setBackgroundColor(context.getResources().getColor(
				R.color.transparent));

	}

}